﻿using System;
using System.Collections.Generic;
using CvModel;
using DAL;

namespace LAB3_Cv
{
    public partial class WorkAd : System.Web.UI.Page
    {
        private int _workAdId;
        private Applicant _currentUser;
        private List<WorkApplications> _applications;
        private ContactPerson _contact;
        private bool _hasApplied;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["WorkAdId"] != null)
                {

                    _workAdId = int.Parse(Session["WorkAdId"].ToString());
                    _contact = ContactPersonDB.GetContactPerson(_workAdId);
                    LoadWorkAd();
                    LoadCompany();
                    LoadContactPerson();
                }
                if (Session["Applications"] != null)
                {
                    _applications = Session["Applications"] as List<WorkApplications>;
                    foreach (var application in _applications)
                    {
                        if (application.WorkAdId == _workAdId)
                        {
                            _hasApplied = true;
                        }
                    }
                }
                if (Session["CurrentUser"] != null)
                {
                    _currentUser = Session["CurrentUser"] as Applicant;
                    if (_currentUser.RegistredCv == true && _hasApplied == false)
                    {
                        ButtonApplyForWork.Visible = true;
                    }
                    else if (_currentUser.RegistredCv == false && _hasApplied == false)
                    {
                        LabelNoCv.Visible = true;
                    }
                    else
                    {
                        LabelApplied.Visible = true;
                    }
                }

            }
            if (IsPostBack)
            {
                _currentUser = Session["CurrentUser"] as Applicant;
                _workAdId = int.Parse(Session["WorkAdId"].ToString());
                _contact = ContactPersonDB.GetContactPerson(_workAdId);

                if (Session["Applications"] != null)
                {
                    _applications = Session["Applications"] as List<WorkApplications>;
                    foreach (var application in _applications)
                    {
                        if (application.WorkAdId == _workAdId)
                        {
                            _hasApplied = true;
                            ButtonApplyForWork.Visible = false;
                            LabelApplied.Visible = true;
                        }
                    }
                }

            }
        }

        public void LoadWorkAd()
        {
            var workAd = WorkAdDB.GetWorkAd(_workAdId);

            LabelProfession.Text = workAd.Proffession;
            LabelCity.Text = workAd.City;
            LabelDateAdded.Text = workAd.DateAdded.ToShortDateString();
            LabelDayOfApplication.Text = workAd.DayOfApplication.ToShortDateString();
            TextBoxWorkDescriptions.Text = workAd.WorkDescription;
            TextBoxQualifications.Text = workAd.Qualifications;
            LabelSalary.Text = workAd.Salary;
            LabelDuration.Text = workAd.Duration;

        }

        public void LoadContactPerson()
        {
            LabelContactEmail.Text = _contact.Email;
            LabelContactName.Text = string.Format("{0} {1}", _contact.FirstName, _contact.LastName);
            LabelContactPhone.Text = _contact.Phone;
            LabelContactTitle.Text = _contact.Title;
        }

        public void LoadCompany()
        {
            var admin = AdminDB.GetCompany(_workAdId);
            LabelCompanyName.Text = admin.CompanyName;
            LabelCompanyAddress.Text = admin.Address;
            LabelCompanyCityZip.Text = string.Format("{0} {1}", admin.Zip, admin.City);
        }

        protected void ApplyForWork_ButtonApplyForWork_Click(object sender, EventArgs e)
        {
            var application = new WorkApplications()
            {
                IsDeleted = false,
                WorkAdId = _workAdId,
                ApplicantId = _currentUser.ApplicantId
            };
            WorkApplicationsDB.CreateWorkApplication(application);
            _applications = Session["Applications"] as List<WorkApplications>;
            _applications.Add(application);
            Session["Applications"] = _applications;
            ButtonApplyForWork.Visible = false;
            LabelApplied.Visible = true;
        }

        protected void ButtonAllAdsFromCompany_Click(object sender, EventArgs e)
        {
            Session["AdminId"] = _contact;
            Response.Redirect("CompanyAds.aspx");
        }
    }
}